// 创建 XMLHttpRequest 对象
function createXHR() {
    if (typeof window !== 'undefined') {
        return new window.XMLHttpRequest();
    } else {
        throw new Error('Axios is not supported in this environment');
    }
}

function MyXHR (config = {}) {
    return new Promise((resolve, reject) => {
        const methed = config.methed || 'GET'
        const url = config.url || ''
        const headers = config.headers || {}
        const xhr = createXHR();
        const data = config.data || {}
        // 设置请求类型为 GET
        xhr.open(methed, url);
        // 设置请求头
        for (const key in headers) {
            if (Object.hasOwnProperty.call(headers, key)) {
                const value = headers[key];
                xhr.setRequestHeader(key, value)
            }
        }
        // 注册事件处理程序来处理服务器返回的数据
        xhr.onload = function () {
            if (xhr.status === 200) {
                const data = JSON.parse(xhr.responseText)
                console.log(data);
                resolve(data)
            } else {
                console.error(`Error ${xhr.status}: ${xhr.statusText}`);
                reject(`Error ${xhr.status}: ${xhr.statusText}`);
            }
        };
        // 发送请求
        xhr.send(JSON.stringify(data));
    })
}